﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	internal static class GenParse_ReportType_General
	{
		#region private constants

		internal const byte MsgBlockOnlyValue = 0x00;

		private const byte RevisionByte = 0x01;
		private const byte MessageByte = 0x00;

		#endregion private constants

		#region internal methods

		internal static byte GenerateByte(ReportType_General reportTypeGeneral)
		{
			byte resultByte = 0x0;
			
			// add Bits 6,5,4
			resultByte |= MsgBlockOnlyValue;

			// add the report type sub type
			if (reportTypeGeneral.ReportSubType == eReportSubType_General.Message)
			{
				resultByte |= MessageByte;
				return resultByte;
			}

			if (reportTypeGeneral.ReportSubType == eReportSubType_General.Revision)
			{
				resultByte |= RevisionByte;
				return resultByte;
			}

			// TODO handle error (unknown eReportSubType_General value)
			return resultByte;
		}

		internal static bool ParseByte(byte byteValue, out eReportSubType_General reportSubTypeGeneral)
		{
			// set arbitary default value
			reportSubTypeGeneral = eReportSubType_General.Message;

			// get only bits 6,5,4
			byte messageBlock = (byte) (byteValue & (byte) 0x70);
			if (messageBlock == MsgBlockOnlyValue)
			{
				// get only bits 3,2,1,0
				byte subType = (byte) (byteValue & (byte) 0x0f);
				if (subType == RevisionByte)
				{
					reportSubTypeGeneral = eReportSubType_General.Revision;
					return true;
				}

				if (subType == MessageByte)
				{
					reportSubTypeGeneral = eReportSubType_General.Message;
					return true;
				}

				// bits 3,2,1,0 are wrong
				return false;
			}

			// bits 6,5,4 are wrong
			return false;
		}

		#endregion internal methods
	}
}
